package com.reactnativeijkplayerlj;

import android.util.Log;

import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.common.MapBuilder;
import com.facebook.react.uimanager.SimpleViewManager;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.annotations.ReactProp;

import java.util.Map;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;


public class IJKPlayerViewManager extends SimpleViewManager<IJKPlayer> {
    private static final String TAG = "IJKPlayerViewManager";
    private final String REACT_CLASS = "IJKPlayer";
    private static final int COMMAND_PAUSE_ID = 1;
    private static final String COMMAND_PAUSE_NAME = "pause";
    private static final int COMMAND_PLAY_ID = 2;
    private static final String COMMAND_PLAY_NAME = "play";
    private static final int COMMAND_STOP_ID = 3;
    private static final String COMMAND_STOP_NAME = "stop";
    private static final int COMMAND_SEEK_TO_ID = 4;
    private static final String COMMAND_SEEK_TO_NAME = "seekTo";

    @Nonnull
    @Override
    public String getName() {
        return REACT_CLASS;
    }

    @Nonnull
    @Override
    protected IJKPlayer createViewInstance(@Nonnull ThemedReactContext reactContext) {
        IJKPlayer ijkPlayer = new IJKPlayer(reactContext);
        return ijkPlayer;
    }

    @ReactProp(name = "url")
    public void setUrl(IJKPlayer ijkPlayer, String url) {
        Log.i(TAG, "url:" + url);
        if (ijkPlayer.isPlaying()) {
            ijkPlayer.restart(url);
        } else {
            if (!url.equals("")) {
                ijkPlayer.setDataSource(url);
            }
        }
    }

    @javax.annotation.Nullable
    @Override
    public Map<String, Integer> getCommandsMap() {
        return MapBuilder.of(
                COMMAND_PAUSE_NAME, COMMAND_PAUSE_ID,
                COMMAND_PLAY_NAME, COMMAND_PLAY_ID,
                COMMAND_STOP_NAME, COMMAND_STOP_ID,
                COMMAND_SEEK_TO_NAME, COMMAND_SEEK_TO_ID
        );
    }

    @Override
    public void receiveCommand(@Nonnull IJKPlayer root, int commandId, @javax.annotation.Nullable ReadableArray args) {
        switch (commandId) {
            case COMMAND_PAUSE_ID:
                root.pause();
                break;
            case COMMAND_PLAY_ID:
                root.start();
                break;
            case COMMAND_STOP_ID:
                root.stop();
                break;
            case COMMAND_SEEK_TO_ID:
                int progress = args.getInt(0);
                root.seekTo(progress);
                break;
            default:
                break;
        }

    }

    @Nullable
    @Override
    public Map getExportedCustomBubblingEventTypeConstants() {
        Log.i(TAG, "getExPorted::::");
        return MapBuilder.builder()
                .put(
                        "onReceiveRatioHW",
                        MapBuilder.of(
                                "phasedRegistrationNames",
                                MapBuilder.of("bubbled", "onReceiveRatioHW")))
                .put(
                        "onReceiveDuration",
                        MapBuilder.of(
                                "phasedRegistrationNames",
                                MapBuilder.of("bubbled", "onReceiveDuration")))
                .put(
                        "onInfo",
                        MapBuilder.of(
                                "phasedRegistrationNames",
                                MapBuilder.of("bubbled", "onInfo")))
                .put(
                        "onError",
                        MapBuilder.of(
                                "phasedRegistrationNames",
                                MapBuilder.of("bubbled", "onError")))
                .put(
                        "onPrepared",
                        MapBuilder.of(
                                "phasedRegistrationNames",
                                MapBuilder.of("bubbled", "onPrepared")))
                .put(
                        "onProgressUpdate",
                        MapBuilder.of(
                                "phasedRegistrationNames",
                                MapBuilder.of("bubbled", "onProgressUpdate")))
                .build();
    }

}
